<template>
  <z-modal
      v-bind="modalOption"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-tabs default-active-key="1" @change="callback">
        <a-tab-pane key="1" tab="订单信息">
          <a-form-model layout="horizontal" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form">
            <a-row>
              <a-col :span="12" >
                <a-form-model-item label="订单编号" prop="orderNo" >
                  <a-input v-model.trim="form.orderNo" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="姓名" prop = "wxName" >
                  <a-input v-model.trim="form.wxName" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="电话" prop = "wxPhone" >
                  <a-input v-model.trim="form.wxPhone" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="单位名称" prop = "wxCompany" >
                  <a-input v-model.trim="form.wxCompany" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="业务类型" prop = "bizType" >
                  <z-dict-select-tag  v-model="form.bizType" placeholder="请选择业务类型" dictCode="bizType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="颜色" prop = "printColor">
                  <z-dict-select-tag  v-model="form.printColor" placeholder="请选择颜色" dictCode="printColor" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='30'">
                <a-form-model-item label="颜色" prop = "bannerColor">
                  <z-dict-select-tag  v-model="form.bannerColor" placeholder="请选择颜色" dictCode="bannerColor" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="尺寸" prop = "size">
                  <z-dict-select-tag  v-model="form.size" placeholder="请选择尺寸" dictCode="size" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="打印类型" prop = "printType">
                  <z-dict-select-tag  v-model="form.printType" placeholder="请选择打印类型" dictCode="printType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="纸张类型" prop = "printPaperType">
                  <z-dict-select-tag  v-model="form.printPaperType" placeholder="请选择纸张类型" dictCode="printPaperType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='20'">
                <a-form-model-item label="纸张类型" prop = "cardPaperType">
                  <z-dict-select-tag  v-model="form.cardPaperType" placeholder="请选择纸张类型" dictCode="cardPaperType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'||form.bizType==='20'">
                <a-form-model-item label="数量" prop = "num">
                  <a-input v-model.trim="form.num" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="装订方式" prop = "bindType">
                  <z-dict-select-tag  v-model="form.bindType" placeholder="请选择装订方式" dictCode="bindType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='10'">
                <a-form-model-item label="装订本数" prop = "bindNum">
                  <a-input v-model.trim="form.bindNum" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='30'">
                <a-form-model-item label="长度" prop = "length">
                  <a-input v-model.trim="form.length" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='30'">
                <a-form-model-item label="内容" prop = "content">
                  <a-input v-model.trim="form.content" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="form.bizType==='50'">
                <a-form-model-item label="设计类型" prop = "designType">
                  <z-dict-select-tag  v-model="form.designType" placeholder="请选择设计类型" dictCode="designType" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="期望交付日期" prop = "expectDate" >
                  <a-input v-model.trim="form.expectDate" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" >
                <a-form-model-item label="其他需求" prop = "otherDemand" >
                  <a-input v-model.trim="form.otherDemand" type="textarea" :disabled="disableSubmit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="12" v-if="getEnable">
                <a-form-model-item label="预览图">
                  <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
                    <a-button> <a-icon type="upload" /> 选择文件</a-button>
                    <span style="color: #409eff;">请只上传样品，注意保护版权</span>
                  </a-upload>
                  <a-popconfirm title="确定要上传吗？" ok-text="确定" cancel-text="再想想"
                    @confirm="handleUpload('previewImage')"
                    :disabled="fileList.length === 0">
                    <a-button
                        type="primary"
                        :loading="uploading"
                        :disabled="fileList.length === 0"
                        style="margin-top: 16px"
                    >
                      {{ uploading ? '上传中' : '开始上传' }}
                    </a-button>
                  </a-popconfirm>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-tab-pane>
        <a-tab-pane key="2" tab="订单日志">
          <a-row>
            <a-col :span="8">
              <a-timeline style="width: 300px;">
                <a-timeline-item v-for="item in logList" :key="item.id">
                  <span style="font-size: 16px">{{item.createTime}}</span>
                  <div style="color: #2a7dc9; margin-left: 8px;">
                    {{item.content}}
                    <a-button type="primary" size="small" v-if="item.fileNo" :loading="imgLoading" @click="viewImage(item.fileNo)">预览</a-button>
                  </div>
                </a-timeline-item>
              </a-timeline>
            </a-col>
            <a-col :span="16">
              <img :src="imagedata" style="width: 80%;">
            </a-col>
          </a-row>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </z-modal>
</template>

<script>
import { Timeline } from 'ant-design-vue'
import ModalFormMixin from '@/mixins/ModalFormMixin'
import ZDictSelectTag from '@/components/dict/ZDictSelectTag'
import { Upload as AUpload } from 'ant-design-vue'
import { postAction, getAction } from '@/services/manage'
import { mapGetters } from 'vuex'
export default {
  name: 'OrderDetail',
  mixins: [ModalFormMixin],
  components: {
    ZDictSelectTag,
    AUpload,
    [Timeline.name]: Timeline,
    [Timeline.Item.name]: Timeline.Item
  },
  data() {
    return {
      isdesigner: false,
      fileList: [],
      url: {
        edit: '/doc/order/v1/edit'
      },
      uploading: false,
      uploadAction: '/doc/order/v1/upload',
      logList: [],
      viewVisible: false,
      imgLoading: false,
      imagedata: ''
    }
  },
  computed: {
    ...mapGetters(['userinfo']),
    getEnable() {
      const auths = this.userinfo.authorities
      auths.forEach(auth => {
        if (auth.roleId === 'fd388b97-673e-4595-ba60-d310aa2ba019') {
          this.isdesigner = true
          return
        }
      })
      return this.isdesigner
    }
  },
  methods: {
    handleRemove(file) {
      const index = this.fileList.indexOf(file)
      const newFileList = this.fileList.slice()
      newFileList.splice(index, 1)
      this.fileList = newFileList
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file]
      return false
    },
    handleUpload(field) {
      const { fileList } = this
      if (fileList.length > 1) {
        this.$message.warn('只可选择一张预览图')
        return
      }
      this.doUpload(field)
    },
    doUpload(field) {
      const { fileList } = this
      const formData = new FormData()
      formData.append('file', fileList[0])
      formData.append('orderNo', this.form.orderNo)
      formData.append('field', field)
      this.uploading = true
      postAction(this.uploadAction, formData).then(res => {
        this.uploading = false
        if (res.success) {
          this.$message.success('上传成功')
          this.$emit('ok')
          this.close()
        } else {
          this.$message.error('上传失败：' + res.message)
        }
      })
    },
    callback(key) {
      if (key === '2') {
        if (this.logList.length === 0) {
          this.confirmLoading = true
          getAction('/doc/orderlog/v1/list?orderId=' + this.form.id).then(res => {
            this.confirmLoading = false
            if (res.success) {
              this.logList = res.result
            } else {
              this.$message.error('获取订单变动日志失败，请稍后重试')
            }
          })
        }
      }
    },
    viewImage(fileNo) {
      this.imgLoading = true
      getAction('/doc/order/v1/getPreviewImage?fileNo=' + fileNo).then(res => {
        this.imgLoading = false
        if (res.success) {
          this.imagedata = res.result
        } else {
          this.$message.error('图片加载失败，请稍后重试')
        }
      })
    }
  }
}
</script>
